import { httpGet, httpPost, httpPut, httpDelete } from '@/utils/http';
import type { Tenant, QueryParams, Pagination } from '@/types';

// 租户列表查询参数
export interface TenantListParams extends QueryParams {
  name?: string;
  code?: string;
  status?: 'active' | 'inactive' | 'suspended';
}

// 租户列表响应
export interface TenantListResult {
  list: Tenant[];
  pagination: Pagination;
}

// 创建租户参数
export interface CreateTenantParams {
  name: string;
  code: string;
  description?: string;
  logo?: string;
  features: string[];
  settings: Record<string, any>;
}

// 更新租户参数
export interface UpdateTenantParams extends Partial<CreateTenantParams> {
  id: string;
}

/**
 * 获取租户列表
 */
export const getTenantList = (params: TenantListParams) => {
  return httpGet<TenantListResult>('/tenant/list', params);
};

/**
 * 获取租户详情
 */
export const getTenantDetail = (id: string) => {
  return httpGet<Tenant>(`/tenant/${id}`);
};

/**
 * 创建租户
 */
export const createTenant = (params: CreateTenantParams) => {
  return httpPost<Tenant>('/tenant', params);
};

/**
 * 更新租户
 */
export const updateTenant = (params: UpdateTenantParams) => {
  const { id, ...data } = params;
  return httpPut<Tenant>(`/tenant/${id}`, data);
};

/**
 * 删除租户
 */
export const deleteTenant = (id: string) => {
  return httpDelete(`/tenant/${id}`);
};

/**
 * 启用租户
 */
export const enableTenant = (id: string) => {
  return httpPost(`/tenant/${id}/enable`);
};

/**
 * 禁用租户
 */
export const disableTenant = (id: string) => {
  return httpPost(`/tenant/${id}/disable`);
};

/**
 * 暂停租户
 */
export const suspendTenant = (id: string) => {
  return httpPost(`/tenant/${id}/suspend`);
};

/**
 * 获取租户配置
 */
export const getTenantConfig = (tenantId: string) => {
  return httpGet<Record<string, any>>(`/tenant/${tenantId}/config`);
};

/**
 * 更新租户配置
 */
export const updateTenantConfig = (tenantId: string, config: Record<string, any>) => {
  return httpPut(`/tenant/${tenantId}/config`, config);
};

/**
 * 获取租户统计信息
 */
export const getTenantStats = (tenantId: string) => {
  return httpGet<{
    userCount: number;
    activeUsers: number;
    storageUsed: number;
    apiCalls: number;
  }>(`/tenant/${tenantId}/stats`);
};

/**
 * 检查租户代码是否可用
 */
export const checkTenantCode = (code: string) => {
  return httpGet<{ available: boolean }>('/tenant/check-code', { code });
}; 